package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Child;
import cn.project.lemontree.bean.Parent;
import cn.project.lemontree.bean.User;
import cn.project.lemontree.service.ChildService;
import cn.project.lemontree.service.Notice_PerService;
import cn.project.lemontree.service.ParentService;
import cn.project.lemontree.tools.CommonResult;
import cn.project.lemontree.utils.MyAuthenticationToken;
import com.alibaba.excel.util.StringUtils;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/parent")
@Api(tags = "家长管理模块")
@CrossOrigin
public class ParentController {
    @Resource
    private ParentService parentService;
    @Resource
    private Notice_PerService notice_perService;
    @Resource
    private ChildService childService;

    @ApiOperation("分页查询家长")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="parentName",value="家长名称",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="telephone",value="电话号码",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult listpage(int page,int size,String parentName,String telephone){
        Map<String,Object> map = new HashMap<>();
        map.put("count",parentService.count(parentName,telephone));
        map.put("data",parentService.listpage(page,size,parentName,telephone));
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除家长")
    @ApiOperation("删除家长信息")
    @GetMapping("/remove")
    public CommonResult remove(@RequestParam("parent_id") int parent_id){
        return CommonResult.success(parentService.removeById(parent_id));
    }

    @SystemControllerLog(description = "添加家长")
    @ApiOperation("添加家长信息")
    @PostMapping("/save")
    public CommonResult save(@RequestBody Parent parent) {
        return CommonResult.success(parentService.save(parent));
    }

    @ApiOperation(value = "家长登录")
    @PostMapping("/parent_login")
    public CommonResult login(@RequestBody Parent parent){

        char[] pass = parent.getPassword().toCharArray();
        MyAuthenticationToken token = new MyAuthenticationToken(parent.getTelephone(),pass,0);
        Subject subject = SecurityUtils.getSubject();
        try{
            if(!subject.isAuthenticated()){
                subject.login(token);
            }
        }catch (UnknownAccountException e){
            return CommonResult.failed("用户名不存在");
        }catch (IncorrectCredentialsException e){
            return CommonResult.failed("密码不正确");
        }catch (AuthenticationException e){
            return CommonResult.failed();
        }
        return CommonResult.success(true);
    }

    @SystemControllerLog(description = "修改家长")
    @ApiOperation("修改家长信息")
    @PostMapping("/update")
    public CommonResult update(@RequestBody Parent parent){
        return CommonResult.success(parentService.update(parent));
    }

    @GetMapping("/notice")
    public CommonResult<Map<String ,Object>> searchNotice(Integer parentId){

        Map<String ,Object> map = new HashMap<>();
        map.put("data",notice_perService.searchNotice(parentId,0));
        map.put("count",notice_perService.count(parentId,0));
        return CommonResult.success(map);
    }

    @GetMapping("/findMyChild")
    public CommonResult findMyChild(Integer parentId){
//        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Map<String ,Object> map = new HashMap<>();
        List<Child> children = childService.findChild(parentId);
        map.put("data",children);
        map.put("count",childService.countChild(parentId));
        map.put("countClass",childService.countClass(children));
        return CommonResult.success(map);
    }
}

